package edu.usc.tweetool.logic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

import edu.usc.tweetool.hibernate.Follow;
import edu.usc.tweetool.hibernate.FollowDAO;
import edu.usc.tweetool.hibernate.FollowId;
import edu.usc.tweetool.hibernate.HighQueue;
import edu.usc.tweetool.hibernate.HighQueueDAO;
import edu.usc.tweetool.hibernate.RegUser;
import edu.usc.tweetool.hibernate.RegUserDAO;
import edu.usc.tweetool.hibernate.UserThetaDAO;

import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

public class LoginProcess {

	private static LoginProcess login = null;

	public static String REDIRECT_URL = null;

	public static LoginProcess getInstance() {
		if (login == null) {
			login = new LoginProcess();
		}
		return login;
	}

	private Twitter twitter = null;
	public Long id = null;
	public String username = null;
	private RequestToken requestToken = null;
	private boolean fault;

	private LoginProcess() {
		fault = false;
	}

	public void init() throws TwitterException {
		// File file = new File("twitter4j.properties");
		Properties prop = new Properties();
		InputStream is = null;
		OutputStream os = null;

		// if (file.exists()) {
		// is = new FileInputStream(file);
		// prop.load(is);
		// }
		prop.setProperty("oauth.consumerKey", UserProcess.CUSTOMER_KEY);
		prop.setProperty("oauth.consumerSecret", UserProcess.CUSTOMER_SECRET);

		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey(UserProcess.CUSTOMER_KEY)
				.setOAuthConsumerSecret(UserProcess.CUSTOMER_SECRET);

		twitter = new TwitterFactory(cb.build()).getInstance();
		requestToken = twitter.getOAuthRequestToken();
		System.out.println("Got request token.");
		System.out.println("Request token: " + requestToken.getToken());
		System.out.println("Request token secret: "
				+ requestToken.getTokenSecret());
		REDIRECT_URL = requestToken.getAuthorizationURL();
	}

	public boolean login(String pin) {
		if (!fault) {
			try {
				init();
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		AccessToken accessToken = null;
		// accessToken = twitter.getOAuthAccessToken(requestToken);
		try {
			if (pin != null && !pin.equals("")) {
				System.out.println("Pin: " + pin);
				accessToken = twitter.getOAuthAccessToken(requestToken, pin);
			} else {
				accessToken = twitter.getOAuthAccessToken(requestToken);
			}
		} catch (TwitterException te) {
			System.out.println("Redirect");
			fault = true;
			return false;
		}
		System.out.println("Got access token.");
		System.out.println("Access token: " + accessToken.getToken());
		System.out.println("Access token secret: "
				+ accessToken.getTokenSecret());
		UserProcess.ACCESS_KEY = accessToken.getToken();
		UserProcess.ACCESS_SECRET = accessToken.getTokenSecret();
		try {
			id = twitter.getId();
			username = twitter.getScreenName();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		fault = false;
		return true;
	}

	public Twitter getTwitter() {
		return twitter;
	}

}
